package org.tang.springcloudvv.util;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tang.springcloudvv.cache.CacheMqQueues;
import org.tang.springcloudvv.config.CommonTimerTask;
import org.tang.springcloudvv.model.Log4jSlowSql;
import org.tang.springcloudvv.request.RequestEvent;
import org.tang.springcloudvv.request.RequestThreadLocal;

import java.util.Date;

/**
 * <p><em>Created on 2022/10/13 2:57 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
public class SqlUtil {

    protected static final Logger logger = LoggerFactory.getLogger(SqlUtil.class);

    private static  String select = "select";
    private static String update = "update";
    private static String delete = "delete";

    public static void checkSlowSql(String sql,long useTime,String sqlId)
    {
        if (check(sql))
        {
            if (useTime >= CommonTimerTask.SLOW_SQL_TIME_)
            {
                logger.info("slowsql[slow]: usedTime=["+useTime+"],{},{}",sqlId,sql);
                SqlUtil.addSlowSQL(useTime,sql,sqlId);
            }
            else
            {
                logger.info("slowsql[quickly]: usedTime=["+useTime+"],{},{}",sqlId,sql);
            }
        }
        else
        {
            logger.info("slowsql: check=false");
        }
    }


    public static void addSlowSQL(long useTime, String sql,String sqlId)
    {
        Log4jSlowSql log4jSlowSql = new Log4jSlowSql();
        sqlId = dealSqlId(sqlId);
        log4jSlowSql.setSqlid(sqlId);
        log4jSlowSql.setUserId(0L);

        //log4jSlowSql.setFromApp(AppContext.app_name);
        log4jSlowSql.setUsedtime(useTime);
        log4jSlowSql.setSqlContent(sql);
        log4jSlowSql.setCreateTime(new Date());

        RequestEvent eventPath = RequestThreadLocal.getEventPath();
        if (eventPath != null)
        {
            log4jSlowSql.setUserId(eventPath.getUserId());
            log4jSlowSql.setUserName(eventPath.getUserName());
            log4jSlowSql.setEventPath(eventPath.getPath());
            log4jSlowSql.setRequestId(eventPath.getRequestId());
        }

        CacheMqQueues.add(CacheMqQueues.log4jSlowSql,log4jSlowSql);
    }

    //String sqlId = "CommonConfigMapper.selectByExample";
    //返回CommonConfigMapper.selectByExample
    public static String dealSqlId(String sqlId)
    {
        if (StringUtils.isEmpty(sqlId) || sqlId.length()<10)
        {
            return sqlId;
        }
        String temp = sqlId;
        int index = temp.lastIndexOf(".");
        if (index < 1)
        {
            return sqlId;
        }
        temp = temp.substring(0,index);
        index = temp.lastIndexOf(".");
        if (index < 1)
        {
            return sqlId;
        }
        temp = temp.substring(0,index);
        sqlId = sqlId.substring(temp.length()+1);

        if (sqlId.length()>200)
        {
            sqlId = sqlId.substring(sqlId.length()-200,sqlId.length());
        }

        return sqlId;
    }

    private static boolean check(String sql)
    {
        if (sql == null)
        {
            return false;
        }

        if (StringUtils.isNotEmpty(sql) && sql.length()>30)
        {
            sql = sql.toLowerCase();
            if (sql.contains(select)
                    || sql.contains(update)
                    || sql.contains(delete))
            {
                return true;
            }
        }
        return false;
    }
}
